package idv.kylechen.mathpractice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MathQuizActivity extends Activity {
    /** Called when the activity is first created. */
	int iNum1Digit;
	int iNum2Digit;
	int iOperator;
	int iOperMode;
	int iQuizCount;
	int iAnswered;
	int iCorrect;
	int iWrong;
	int iEndMode;
	int iNumber1, iNumber2;
	int iOffset1, iOffset2;
	int iRange1, iRange2;
	TextView tvAnswered, tvRemainder, tvCorrect, tvWrong, tvScore, tvTime, tvPrevious;
	TextView tvNumber1, tvNumber2, tvQuizNum;
	Button btnEnd;
	EditText edtAnswer;
	private Long startTime;
	private Handler handler = new Handler();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mathquiz);  
        Intent intent = this.getIntent();
        Bundle extra = intent.getExtras();
        iNum1Digit = extra.getInt("Number1Digit");
        iNum2Digit = extra.getInt("Number2Digit");
        iQuizCount = extra.getInt("QuizCount");
        iOperMode = extra.getInt("Operator");
    	tvAnswered = (TextView) findViewById(R.id.tvAnswered);
    	tvRemainder = (TextView) findViewById(R.id.tvRemainder);
    	tvCorrect = (TextView) findViewById(R.id.tvCorrect);
    	tvWrong = (TextView) findViewById(R.id.tvWrong);
    	tvScore = (TextView) findViewById(R.id.tvScore);
    	tvTime = (TextView) findViewById(R.id.tvTime);
    	tvPrevious = (TextView) findViewById(R.id.tvPrevious);
    	btnEnd = (Button) findViewById(R.id.btnEnd);
    	tvQuizNum = (TextView) findViewById(R.id.tvQuizNumber);
    	tvNumber1 = (TextView) findViewById(R.id.tvNumber1);
    	tvNumber2 = (TextView) findViewById(R.id.tvNumber2);
    	edtAnswer = (EditText) findViewById(R.id.edtAnswer);
        handler.removeCallbacks(updateTimer);
        handler.postDelayed(updateTimer, 1000);       	
        StartQuiz();
    }
    
    private void StartQuiz() {
    	iAnswered = 0;
    	iCorrect = 0;
    	iWrong = 0;
    	tvAnswered.setText(Integer.toString(iAnswered));
    	tvRemainder.setText(Integer.toString(iQuizCount-iAnswered));    
    	tvCorrect.setText(Integer.toString(iCorrect));
    	tvWrong.setText(Integer.toString(iWrong));    
    	tvScore.setText("");   
    	tvTime.setText("");   
    	tvPrevious.setText("");   
    	btnEnd.setText(getString(R.string.strSkip));
    	iOffset1 = (int) (Math.pow(10, iNum1Digit-1)+0.005);
    	iOffset2 = (int) (Math.pow(10, iNum2Digit-1)+0.005);
    	iRange1 = (int) (Math.pow(10, iNum1Digit)- Math.pow(10, iNum1Digit-1) + 0.005);
    	iRange2 = (int) (Math.pow(10, iNum2Digit)- Math.pow(10, iNum2Digit-1) + 0.005);    	
        startTime = System.currentTimeMillis(); 	
    	MakeQuiz();
    }
    
    private void CheckAnswer()  {
    	boolean bCorrect = true;
    	String sAnswer = String.valueOf(edtAnswer.getText());
    	if (sAnswer.trim().isEmpty()) {
    		ShowMessage(this.getString(R.string.strErrEmpty));
    		return;
        }

    	int iAnswer = Integer.parseInt(sAnswer);
    	int iCorAns = 0;
    	if (iOperator==0) {
    		iCorAns = iNumber1 + iNumber2;
    		if (iAnswer == iCorAns ) {
    			bCorrect = true;
    		}
    		else {
    			bCorrect = false;
            }
    	}
    	else {
    		iCorAns = iNumber1 - iNumber2;
    		if (iAnswer == iCorAns) {
            	bCorrect = true;
    		}
    		else {
            	bCorrect = false;
            }
    	}

    	if (bCorrect) {
    		iCorrect++;
    		tvCorrect.setText(String.valueOf(iCorrect));
    		tvPrevious.setTextColor(0xff00ff00);
    		tvPrevious.setText(this.getString(R.string.strMsgCorrect));
    	}
    	else {
    		String sMsg = String.format(this.getString(R.string.strMsgWrongPrompt), iCorAns);
    		ShowMessage(sMsg);
    		iWrong++;
    		tvWrong.setText(String.valueOf(iWrong));
    		tvPrevious.setTextColor(0xffff0000);
    		tvPrevious.setText(this.getString(R.string.strMsgWrong));
    	}
    	iAnswered++;
    	tvAnswered.setText(String.valueOf(iAnswered));
    	tvRemainder.setText(String.valueOf(iQuizCount-iAnswered)); 
    	if (iAnswered < iQuizCount)
    		MakeQuiz();
    	else {
    		EndQuiz();
    	}
    }   
    
    
    private void MakeQuiz() {
    	if (iOperMode==0)
    		iOperator = (int) (Math.random()*2);
    	else
    		iOperator = iOperMode - 1;

    	iNumber1 = (int) (Math.random()*iRange1 + iOffset1);
    	iNumber2 = (int) (Math.random()*iRange2 + iOffset2);
    	if ((iOperator==1) && (iNumber2>iNumber1)) {
    		int t = iNumber2;
    		iNumber2 = iNumber1;
    		iNumber1 = t;
        }

    	tvNumber1.setText(Integer.toString(iNumber1));
    	if (iOperator==0)
    		tvNumber2.setText("+   "+iNumber2);
    	else
    		tvNumber2.setText("-   "+iNumber2);

    	tvQuizNum.setText(this.getString(R.string.strQuiz) + (iAnswered+1));
    	edtAnswer.setText("");
    }
    
    private void EndQuiz() {
    	handler.removeCallbacks(updateTimer);
    	if (iEndMode==0)  {
    		//m_timeEnd = ::GetTickCount();
    		iEndMode = 1;
    		btnEnd.setText(this.getString(R.string.strQuit));
      		int iScore = 0;
    		if (iAnswered>0) {
    			iScore =(int) ((float)iCorrect/(float)iAnswered*100.0 + 0.5);
    			tvScore.setText(String.valueOf(iScore));
            }
    	}
    	else {
        	this.finish();
    	}    	
    }
    
    public void EndClick(View target) {
    	EndQuiz();
    }
    
    public void NextClick(View target) {
    	CheckAnswer();
    }
    
    public void ShowMessage(String message){
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }
    
    private Runnable updateTimer = new Runnable() {
    	public void run() {
    		Long spentTime = System.currentTimeMillis() - startTime;
    		Long minius = (spentTime/1000)/60;
    		Long hour = minius / 60;
    		minius = minius % 60;
    		Long seconds = (spentTime/1000) % 60;
    		String sTime = String.format("%02d:%02d:%02d", hour, minius, seconds);	
    		tvTime.setText(sTime);
    		handler.postDelayed(this, 1000);
    	}
    };    

}
